﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 图书管理.Services;

namespace 图书管理
{
    public partial class BookTypeForm : Form
    {
        BookTypeUtils bookTypeUtils;
        int selId;

        public BookTypeForm()
        {
            InitializeComponent();
            bookTypeUtils = new BookTypeUtils();
        }

        private void BookTypeForm_Load(object sender, EventArgs e)
        {
            RefreshList();
        }
        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton1_Click(object sender, EventArgs e)
        {
            bool v = bookTypeUtils.InsertBookType(uiTextBox1.Text);
            if (v)
            {
                MessageBox.Show("添加成功");
                RefreshList();
            }
        }

        private void RefreshList()
        {
            IEnumerable<Model.BookTyple> enumerable = bookTypeUtils.SelectAll();
            uiDataGridView1.DataSource = enumerable;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton2_Click(object sender, EventArgs e)
        {
            if(selId != 0)
            {
                bool v = bookTypeUtils.EditBookType(selId, uiTextBox1.Text);
                if (v)
                {
                    MessageBox.Show("修改成功");
                    RefreshList();
                }
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton3_Click(object sender, EventArgs e)
        {
            bool v = bookTypeUtils.DeleteBookType(selId);
            if(v)
            {
                MessageBox.Show("删除成功");
                RefreshList();
            }
        }
        /// <summary>
        /// 选择一条记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiDataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            int count = uiDataGridView1.SelectedRows.Count;
            if(count == 1)
            {
                DataGridViewRow row = uiDataGridView1.SelectedRows[0];
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    DataGridViewCell cell = row.Cells[i];
                    if (cell != null)
                    {
                        if (i == 0)
                        {
                            selId = Convert.ToInt32(cell.Value);
                        }
                        else if (i == 1)
                        {
                            uiTextBox1.Text = Convert.ToString(cell.Value);
                        }
                    }
                }
            }
        }
    }
}
